package com.ihighest.app.infox.activity;

import org.json.JSONException;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.RegexKit;

/**
 * 找回密码界面
 * 
 * @author lkx Created at 2015-10-3 下午8:48:42
 * @version 1.0
 */
public class FindPasswordActivity extends BaseActivity implements OnClickListener {
	private ActivityTitle title;
	private EditText phoneNumberEditText;
	private EditText codeEditText;
	private EditText psdEditText;
	private Button getcodeButton;
	private TimeCount timer;
	private int time = 0;
	private String phonenumber;
	private String codeId;// 验证码id

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_findpassword);
		title = (ActivityTitle) findViewById(R.id.infox_findPsdActivity_title);
		title.setTitle("找回密码");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("取消");
		intint();
	}

	/**
	 * 初始化
	 * 
	 * 
	 * @author lkx Created at 2015-10-30 上午10:15:54
	 */
	private void intint() {

		phoneNumberEditText = (EditText) findViewById(R.id.infox_findpsd_userPhone);
		codeEditText = (EditText) findViewById(R.id.infox_findPsd_textcode);
		psdEditText = (EditText) findViewById(R.id.infox_findPsd_inputNewPsd);
		getcodeButton = (Button) findViewById(R.id.infox_findPsd_getCode);
		findViewById(R.id.findPassWordLogin).setOnClickListener(this);

		getcodeButton.setOnClickListener(this);

		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();

			}
		});
	}

	@Override
	public void onClick(View arg0) {

		phonenumber = phoneNumberEditText.getText().toString();
		if (TextUtils.isEmpty(phonenumber) || !RegexKit.isMobile(phonenumber)) {
			ToastKit.showShort(FindPasswordActivity.this, "请输入正确手机号");
			return;
		}

		switch (arg0.getId()) {
		// 获取手机验证码
		case R.id.infox_findPsd_getCode:
			Api.post("mobilecaptcha", ApiParam.create().addParam("phone", phonenumber).addParam("type", "forget"), new ApiCallback(this) {

				@Override
				public void handleSuccess(ApiResult result) {
					try {
						if (result.getCode() == 100) {

							codeId = result.getData().getString("captcha_id");
							time = result.getData().getInt("time");
							long countTime = time * 1000;
							timer = new TimeCount(countTime, 1000);
							getcodeButton.setClickable(false);

							getcodeButton.setBackgroundResource(R.drawable.infox_register_btn_gettextcode_bg);
							timer.start();
						} else {
							ToastKit.showShort(FindPasswordActivity.this, TextUtils.isEmpty(result.getMsg()) ? "获取验证码失败" : result.getMsg());
						}
					} catch (JSONException e) {
						Log.e(TAG, e.getMessage(), e);
					}

				}

				@Override
				public void handleFailure(Exception e, String msg) {
					getcodeButton.setClickable(true);
					getcodeButton.setText("重新发送");
				}
			}, this);

			break;

		case R.id.findPassWordLogin:
			// 登录
			if (codeEditText.getText().toString().equals("") || codeEditText.getText().toString() == null) {
				ToastKit.showShort(this, "请输入验证码");
			} else if (!RegexKit.isPassword(psdEditText.getText().toString())) {
				ToastKit.showShort(this, "密码为6到20位数字或字母");
			} else {
				Api.post("me/forgotPwd",
						ApiParam.create().addParam("phone", phonenumber).addParam("newpass", AppKit.md5(psdEditText.getText().toString()))
								.addParam("captcha", codeEditText.getText().toString()).addParam("captcha_id", codeId), new ApiCallback(
								this) {

							@Override
							public void handleSuccess(ApiResult result) {
								if (result.getCode() == 100) {
									// 找回密码成功
									ToastKit.showShort(FindPasswordActivity.this, "密码设置成功，请登录");
									finish();
								} else {
									ToastKit.showShort(FindPasswordActivity.this,
											TextUtils.isEmpty(result.getMsg()) ? "重设密码失败" : result.getMsg());
								}
							}

							@Override
							public void handleFailure(Exception e, String msg) {
								ToastKit.showShort(FindPasswordActivity.this, msg.toString());
							}
						}, this);
			}
			break;
		}

	}

	@Override
	protected void onDestroy() {
		if (timer != null) {
			timer.cancel();
		}
		super.onDestroy();
	}

	/* 定义一个倒计时的内部类 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			getcodeButton.setText("重新发送");
			getcodeButton.setClickable(true);

			getcodeButton.setBackgroundResource(R.drawable.infox_content_comment_bg);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			getcodeButton.setClickable(false);
			getcodeButton.setText((millisUntilFinished / 1000) + "秒后重新获取");

		}
	}
}
